﻿<#include "/common/easyui-css.html">
<#include "/common/easyui-js.html">
<#include "/common/loading.html">

<script type="text/javascript" src="/lib/layer/2.4/layer.js"></script>

<body class="easyui-layout">

<div data-options="region:'center',border:false" style="padding:2px;height: auto">
	<table id="admin_table" class="easyui-datagrid" title="" style="width:100%;height:100%;padding-bottom: 2px;"
		   data-options="rownumbers:true,
		   				   pagination:true,
		   				   singleSelect:false,
		   				   url:'/user-list-json/1/20',
          				   method:'get',
           			   toolbar:'#tb',
           			   fitColumns : true,
           			   onLoadSuccess:function(data){
                            $('.edit').linkbutton({plain:true,iconCls:'icon-edit'});
                            $('.delete').linkbutton({plain:true,iconCls:'icon-remove'});
                            $('.active').linkbutton({plain:true,iconCls:'icon-add'});
                        },
						   showFooter: true">
        <thead frozen="true">
            <th field="ck" checkbox="true"></th>
            <th data-options="field:'locked',formatter:lockformat,styler:lockCellStyler">锁定</th>
            <th data-options="field:'username',width:200,align:'center'">	登录名称</th>
            <th data-options="field:'nickname',width:200,align:'center'">显示名称</th>
        </thead>
        <thead>
            <tr>
                <th data-options="field:'roleId',width:200,align:'center',formatter:roleformat">角色ID</th>
                <th data-options="field:'belongTo',width:200,align:'center'">选择组别</th>
                <th data-options="field:'powerLevel',width:200,align:'center'">选择职务</th>
                <th data-options="field:'createTime',width:200,align:'center'">创建时间</th>
                <th data-options="field:'id',width:200,formatter:operation,align:'center'">操作</th>
            </tr>
		</thead>
	</table>
</div>

<!-- 操作栏 -->
<div id="tb" style="padding:5px;height:auto">
	<div>
		开始时间: <input class="easyui-datebox" data-options="formatter:dateformatter,parser:dateparser" id="from" style="width:200px">
		结束时间: <input class="easyui-datebox" data-options="formatter:dateformatter,parser:dateparser" id="to" style="width:120px" >
		选择角色:
		<input class="easyui-combobox" name="role"id="role"
			   data-options="
				url : '/role-list',
				method: 'get',
				valueField:'value',
				textField:'text',
				">
        选择组别:
        <input class="easyui-combobox" name="group"id="group"
               data-options="
				url : '/constants/group-types',
				method: 'get',
				valueField:'key',
				textField:'value',
				">
        选择职务:
        <input class="easyui-combobox" name="power"id="power"
               data-options="
				url : '/constants/power-level-types',
				method: 'get',
				valueField:'key',
				textField:'value',
				">
        <a href="#" class="easyui-linkbutton" iconCls="icon-search" id="searchByParameter">搜索</a>
	</div>
    <div style="margin-top: 5px">
        <#include "/common/button.html">
    </div>
</div>


<!-- 格式化时间 -->
<script type="text/javascript">
	function dateformatter(date){
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
	}
	function dateparser(s){
		if (!s) return new Date();
		var ss = (s.split('-'));
		var y = parseInt(ss[0],10);
		var m = parseInt(ss[1],10);
		var d = parseInt(ss[2],10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
			return new Date(y,m-1,d);
		} else {
			return new Date();
		}
	}
    function lockformat(value){
        if(value){
            return '已激活';
        }
        return'未激活';
    }

    function lockCellStyler(value,row,index){
        if (value){
            return 'background-color:#ffee00;color:red;';
        }
    }
    function roleformat(value) {
	    var result = '';
        $.ajax({
            url : '/role-id2name/'+value,
            async : false,
            success : function(data){
                result = data;
            }
        });
        return result;
    }
    function operation(value,row) {
	    var button ='';
	    if(!row.locked){
            button += '<a href="#" class="edit" title="编辑" onclick=\'edit('+value+')\'></a>  <a href="#" class="delete" title="删除" onclick=\'del('+value+')\'></a>';
        }
            button += '<a href="#" class="active" title="激活" onclick=\'active('+value+')\'></a>';
        return button;
    }
</script>

<!-- 条件查询 -->
<script type="text/javascript">
	$("#searchByParameter").click(function(){
		var from = $("#from").val();
		var to = $("#to").val();
		var role = $("#role").val();
		var group = $("#group").val();
		var power = $("#power").val();
		var handler = '/user-list-json/1/10?from='+from+"&to="+to+"&key="+role+"&belongTo="+group+"&powerLevel="+power;
		$('#admin_table').datagrid('options').url = handler;
		$('#admin_table').datagrid('reload');
	});
</script>

<script>

    var url;

    function edit(value){
        var url = '/admin-add?id='+value;
        var index = layer.open({
            title:'用户修改',
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: false,
            content: url
        });
    }
    function del(value){
        layer.confirm('确定删除用户？', {
            btn: ['删除','取消'] //按钮
        }, function(){
            $.ajax({
                url : '/user-ins-upd-del/'+value,
                data : {delete_flag:'Y',tm : new Date().getTime()},
                async : false,
                success : function(){
                    $('#admin_table').datagrid('reload');
                }
            });
            layer.closeAll('dialog');
        }, function(){

        });
    }
    function active(value){
        var ids = [];
        ids.push(value);
        var id = ids.join('@');
        var index = layer.confirm('确定激活/冻结用户？', {
            btn: ['修改','取消'] //按钮
        }, function(){
            $.ajax({
                url : '/user-ins-upd-del/batch/'+id,
                data : {locked:'Y',tm : new Date().getTime()},
                async : false,
                success : function(){
                    $('#admin_table').datagrid('reload');
                }
            });
            layer.closeAll('dialog');
        }, function(){

        });
    }

    //新增
	$('#add').click(function () {
        var url = '/admin-add?id=-1';
        var index = layer.open({
            title:'用户修改',
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: false,
            content: url
        });
    });
	//编辑
	$('#edit').click(function () {
        var row = $('#admin_table').datagrid('getSelected');
        if (row&&!row.locked){
            var url = '/admin-add?id='+row.id;
            var index = layer.open({
                title:'用户修改',
                type: 2,
                area: ['700px', '450px'],
                fixed: false, //不固定
                maxmin: false,
                content: url
            });
        } else if(row&&row.locked){
            layer.msg('激活的用户不能编辑', {icon: 2});
        } else{
            layer.msg('请选择需要编辑的用户', {icon: 2});
        }
    });
	//删除
	$('#delete').click(function () {
        var row = $('#admin_table').datagrid('getSelected');
        if(!row){
            layer.msg('请选择需要删除的用户', {icon: 2});
        }else if(row.locked){
            layer.msg('激活的用户不能删除', {icon: 2});
        //表示所有的数据都可以删除
        }else if(!row.locked){
            layer.confirm('确定删除用户？', {
                btn: ['删除','取消'] //按钮
            }, function(){
                    $.ajax({
                        url : '/user-ins-upd-del/'+row.id,
                        data : {delete_flag:'Y',tm : new Date().getTime()},
                        async : false,
                        success : function(){
                            $('#admin_table').datagrid('reload');
                        }
                    });
                layer.closeAll('dialog');
            }, function(){

            });

        }
    });
	//用户激活
	$('#active').click(function () {

        var ids = [];
        var rows = $('#admin_table').datagrid('getSelections');
        for(var i=0; i<rows.length; i++){
            ids.push(rows[i].id);
        }
        var id = ids.join('@');
        if(rows.length==0){
            layer.msg('请选择需要激活的用户', {icon: 2});
        }else{
            var index = layer.confirm('确定激活/冻结用户？', {
                btn: ['修改','取消'] //按钮
            }, function(){
                $.ajax({
                    url : '/user-ins-upd-del/batch/'+id,
                    data : {locked:'Y',tm : new Date().getTime()},
                    async : false,
                    success : function(){
                        $('#admin_table').datagrid('reload');
                    }
                });
                layer.closeAll('dialog');
            }, function(){

            });
        }
    });
	$('#export').click(function(){
        window.location = "/user-download";
    });
</script>

<script type="text/javascript">
	$(function(){
	    //初始化翻页
		var p = $('#admin_table').datagrid().datagrid('getPager');
		p.pagination({
			pageSize: 20, //每页显示的记录条数，默认为10
			pageList: [20, 25, 35], //可以设置每页记录条数的列表
			onSelectPage: function (pageNumber, pageSize) {
				var handler = '/user-list-json/'+pageNumber+'/'+pageSize;
				$('#admin_table').datagrid('options').url = handler;
				$('#admin_table').datagrid('reload');
			}
		});
		//初始化
	})

</script>

